package commands;

import application.Core;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 *	This class implements command sort.
 * 
 * @author Michal Mourek
 */
public class Sort extends AbstractCommand{
	/**
	 * Main method of this program.
	 * It sorting stndard input or file to standard output.
	 *
	 * @param parameters List of parameters entered by user in command line.
	 * @param arguments List of arguments entered by user in command line.
	 * @param id Process ID.
	 * @param core reference to the core
	 * @author Michal Mourek
	 */
	@Override
	public void main(String[] parameters, String[] arguments, int id, Core core) {
		String workingFolder = core.getDirName(id);
		File verifyPath = new File(workingFolder);
		if (parameters.length == 0){
			if (arguments.length == 0){
				List<String> read = new ArrayList<String>();
				int i = 0;
				while(core.canRead(id)){	
					String s = core.read(id);
					if(s != null){
						read.add(s);
					}
					
				}
				Collections.sort(read);
				for (String string : read) {
					core.write(id,string);
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
			    }
			}else {
				List<String> read = new ArrayList<String>();
				for(int i = 0; i < arguments.length; i++) {
					File dir = new File(arguments[i]);
					if (dir.isFile()){
						try {
							BufferedReader bfr;
							bfr = new BufferedReader(new FileReader(dir));
							try {
								String str = bfr.readLine();
								while(str != null){
									read.add(str);
									str = bfr.readLine();
								}
							} catch (IOException e) {
								core.getGui().writeConsole("IOException");
							}
						} catch (FileNotFoundException e) {
							core.getGui().writeConsole("FileNotFoundException");
						}
						Collections.sort(read);
						for (String string : read) {
							core.write(id,string);
							try {
								Thread.sleep(50);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					} else {
						String[] arrayString = invalidArguments().split("\n");
						for (String string : arrayString) {
							core.write(id, string);
							try {
								Thread.sleep(50);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
				}
			}
				
		} else {
			for(int i = 0; i < parameters.length; i++) {
				char parameter = parameters[i].charAt(1);
				if (parameter == 'h'){
					String[] help = help().split("\n");
					for (String string : help) {
						core.write(id, string);
						try {
							Thread.sleep(50);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					} 
				}else{
					String[] invalidParameters = invalidParameters().split("\n");
					for (String string : invalidParameters) {
						core.write(id, string);
						try {
							Thread.sleep(50);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					break;
				}
			}
		}
	}
	/**
	 * Print help for this command.
	 *
	 * @author Michal Mourek
	 */
	@Override
	public String help() {
		return ("NAME: sort - sorting input \n SYNOPSYS: sort [OPTION] \n \n Description: \n   -h      print help \n");	
	}
	
	/**
	 * Print warning message, when parameter is invalid.
	 *
	 * @author Michal Mourek
	 */
	public String invalidParameters(){
		return ("Parameters is not correct \n Read help sort - h \n");
	}
	
	/**
	 * Print warning message, when argument is invalid.
	 *
	 * @author Michal Mourek
	 */
	public String invalidArguments(){
		return ("Arguments is not correct \n Read help sort - h \n");
	}
}
